using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// TeaOrder 的摘要说明。
	/// </summary>
	public class TeaOrder : BaseDAL<TeaOrderInfo>, ITeaOrder
	{
		#region 对象实例及构造函数

		public static TeaOrder Instance
		{
			get
			{
				return new TeaOrder();
			}
		}
		public TeaOrder() : base("CL_TeaOrder","ID")
		{
		}

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override TeaOrderInfo DataReaderToEntity(IDataReader dataReader)
		{
			TeaOrderInfo teaOrderInfo = new TeaOrderInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			teaOrderInfo.ID = reader.GetInt32("ID");
			teaOrderInfo.OrderNo = reader.GetString("OrderNo");
			teaOrderInfo.RoomNo = reader.GetString("RoomNo");
			teaOrderInfo.RoomType = reader.GetString("RoomType");
            teaOrderInfo.Persons = reader.GetInt32("Persons");
            teaOrderInfo.Waiter = reader.GetString("Waiter");
            teaOrderInfo.CaculateType = reader.GetString("CaculateType");
            teaOrderInfo.AtLeaseMoney = reader.GetDecimal("AtLeaseMoney");
			teaOrderInfo.Price = reader.GetDecimal("Price");
            teaOrderInfo.BuyOutPrice = reader.GetDecimal("BuyOutPrice");
			teaOrderInfo.CustomerName = reader.GetString("CustomerName");
			teaOrderInfo.DepositMoney = reader.GetDecimal("DepositMoney");
			teaOrderInfo.Note = reader.GetString("Note");
			teaOrderInfo.Createor = reader.GetString("Createor");
			teaOrderInfo.CreateDate = reader.GetDateTime("CreateDate");
			teaOrderInfo.IsCheckOut = reader.GetBoolean("IsCheckOut");
			teaOrderInfo.CheckOutDate = reader.GetDateTime("CheckOutDate");
			teaOrderInfo.BillNumber = reader.GetString("BillNumber");
            teaOrderInfo.IsBuyOut = reader.GetBoolean("IsBuyOut");
			
			return teaOrderInfo;
		}

		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(TeaOrderInfo obj)
		{
		    TeaOrderInfo info = obj as TeaOrderInfo;
			Hashtable hash = new Hashtable(); 
			
 			hash.Add("OrderNo", info.OrderNo);
 			hash.Add("RoomNo", info.RoomNo);
 			hash.Add("RoomType", info.RoomType);
            hash.Add("Persons", info.Persons);
            hash.Add("Waiter", info.Waiter);
            hash.Add("CaculateType", info.CaculateType);
            hash.Add("AtLeaseMoney", info.AtLeaseMoney);
 			hash.Add("Price", info.Price);
            hash.Add("BuyOutPrice", info.BuyOutPrice);
 			hash.Add("CustomerName", info.CustomerName);
 			hash.Add("DepositMoney", info.DepositMoney);
 			hash.Add("Note", info.Note);
 			hash.Add("Createor", info.Createor);
 			hash.Add("CreateDate", info.CreateDate);
            hash.Add("IsCheckOut", info.IsCheckOut);
            hash.Add("IsBuyOut", info.IsBuyOut);
            //hash.Add("CheckOutDate", info.CheckOutDate);
            //hash.Add("BillNumber", info.BillNumber);
 				
			return hash;
		}

        /// <summary>
        /// 获取结账单相关的所有订单ID信息
        /// </summary>
        /// <returns></returns>
        public List<int> GetRelatedOrderID(string billNumber)
        {
            string sql = string.Format(@"Select distinct ID from {0} where BillNumber='{1}' and IsCheckOut=1 ", tableName, billNumber);
            List<int> list = new List<int>();

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    int id = Convert.ToInt32(dr["ID"].ToString());
                    list.Add(id);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取当前入住房间的订单信息
        /// </summary>
        /// <param name="roomNo"></param>
        /// <returns></returns>
        public TeaOrderInfo GetCurrentOrder(string roomNo)
        {
            string condition = string.Format("RoomNo='{0}' and IsCheckOut=0 ", roomNo);
            return this.FindSingle(condition);
        }

        /// <summary>
        /// 更新订单的结账状态和时间
        /// </summary>
        /// <param name="Id">订单ID</param>
        /// <param name="isCheckOut">是否结账</param>
        /// <param name="checkOutDate">结账时间</param>
        /// <param name="billNumber">账单号</param>
        /// <returns></returns>
        public bool UpdateCheckOut(int Id, bool isCheckOut, DateTime checkOutDate, string billNumber)
        {
            string sql = string.Format(@"Update {0} set IsCheckOut={1}, CheckOutDate='{2}', 
                                                        BillNumber='{3}' Where ID={4}",
                this.tableName, isCheckOut ? "1" : "0", checkOutDate, billNumber, Id);

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return db.ExecuteNonQuery(command) > 0;
        }

    }
}